package com.tj.collection.db.service.impl;

import com.fqgj.common.utils.CollectionUtils;
import com.tj.collection.db.admin.dao.AuthAdminRoleDAO;
import com.tj.collection.db.admin.dao.AuthMenuDAO;
import com.tj.collection.db.admin.dao.AuthRoleMenuDAO;
import com.tj.collection.db.admin.entity.AuthAdminRoleEntity;
import com.tj.collection.db.admin.entity.AuthMenuEntity;
import com.tj.collection.db.admin.entity.AuthMenuEntityConditions;
import com.tj.collection.db.admin.entity.AuthRoleMenuEntity;
import com.tj.collection.db.domain.Menu;
import com.tj.collection.db.service.AdminMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2017/3/13
 * Time: 下午4:19
 */
@Service
public class AdminMenuServiceImpl implements AdminMenuService {

    @Autowired
    private AuthMenuDAO authMenuDAO;

    @Autowired
    private AuthAdminRoleDAO authAdminRoleDAO;

    @Autowired
    private AuthRoleMenuDAO authRoleMenuDAO;

    @Override
    public List<Menu> getAllMenusByParentId(Long parentId) {
        List<AuthMenuEntity> authMenuEntityList = authMenuDAO.getListByParentId(parentId, null);
        if (CollectionUtils.isEmpty(authMenuEntityList)) {
            return null;
        }
        List<Menu> menuList = new ArrayList<>();
        for (AuthMenuEntity authMenuEntity : authMenuEntityList) {
            Menu menu = new Menu(authMenuEntity.getId(), authMenuEntity.getName(), authMenuEntity.getLink());
            menu.setMenuType(authMenuEntity.getMenuType());
            menuList.add(menu);
        }
        return menuList;
    }

    @Override
    public List<Menu> getAllMenus() {
        AuthMenuEntityConditions authMenuEntityConditions = new AuthMenuEntityConditions();
        authMenuEntityConditions.createCriteria().andDeletedEqualTo(false);
        List<AuthMenuEntity> authMenuEntityList = authMenuDAO.selectByExample(authMenuEntityConditions);

        List<Menu> menuList = new ArrayList<>();
        for (AuthMenuEntity authMenuEntity : authMenuEntityList) {
            Menu menu = new Menu(authMenuEntity.getId(), authMenuEntity.getParent(), authMenuEntity.getName(), authMenuEntity.getLink());
            menu.setParentId(authMenuEntity.getpId());
            menu.setMenuType(authMenuEntity.getMenuType());
            menuList.add(menu);
        }

        return menuList;
    }

    @Override
    public List<Menu> getMenusByIds(List<Long> menuIdsList) {
        List<AuthMenuEntity> authMenuEntityList = authMenuDAO.getListByIds(menuIdsList);
        List<Menu> menuList = new ArrayList<>();
        if(CollectionUtils.isEmpty(authMenuEntityList)){
            return menuList;
        }
        for (AuthMenuEntity authMenuEntity : authMenuEntityList) {
            Menu menu = new Menu(authMenuEntity.getId(), authMenuEntity.getParent(), authMenuEntity.getName(), authMenuEntity.getLink()).setParentId(authMenuEntity.getpId());
            menu.setMenuType(authMenuEntity.getMenuType());
            menuList.add(menu);
        }

        return menuList;
    }

    @Override
    public Boolean add(Menu menu) {
        AuthMenuEntity authMenuEntity = new AuthMenuEntity();
        authMenuEntity.setName(menu.getKey());
        authMenuEntity.setLink(menu.getValue());
        authMenuEntity.setDeleted(false);
        authMenuEntity.setParent(menu.getParent());
        authMenuEntity.setpId(menu.getParentId());
        authMenuEntity.setMenuType(menu.getMenuType());
        return authMenuDAO.save(authMenuEntity) > 0;
    }

    @Override
    public Boolean update(Menu menu) {
        AuthMenuEntity authMenuEntity = authMenuDAO.selectById(menu.getMenuId());
        if (authMenuEntity == null) {
            return false;
        }

        authMenuEntity.setLink(menu.getValue());
        authMenuEntity.setName(menu.getKey());
        authMenuEntity.setMenuType(menu.getMenuType());
        authMenuEntity.setGmtModified(new Date());
        return authMenuDAO.update(authMenuEntity) > 0;
    }

    @Override
    public List<Long> getMenuIdsByAdmin(Long adminId) {
        List<AuthAdminRoleEntity> authAdminRoleEntityList = authAdminRoleDAO.getListByAdminId(adminId);
        if (CollectionUtils.isEmpty(authAdminRoleEntityList)) {
            return null;
        }

        List<Long> roleIds = authAdminRoleEntityList.parallelStream().map(AuthAdminRoleEntity::getRoleId).collect(Collectors.toList());
        List<AuthRoleMenuEntity> authRoleMenuEntityList = authRoleMenuDAO.getListByRoleIds(roleIds);
        if (CollectionUtils.isEmpty(authRoleMenuEntityList)) {
            return null;
        }

        return authRoleMenuEntityList.parallelStream().map(AuthRoleMenuEntity::getMenuId).collect(Collectors.toList());
    }

    @Override
    public List<Long> getMenuIdsByRole(Long roleId) {
        List<Long> roleIds = new ArrayList<>();
        roleIds.add(roleId);
        List<AuthRoleMenuEntity> authRoleMenuEntityList = authRoleMenuDAO.getListByRoleIds(roleIds);
        if (CollectionUtils.isEmpty(authRoleMenuEntityList)) {
            return null;
        }

        return authRoleMenuEntityList.parallelStream().map(AuthRoleMenuEntity::getMenuId).collect(Collectors.toList());
    }


    @Override
    public Boolean addMenuIdToRole(Long roleId, List<Long> menuIds) {
        if (CollectionUtils.isEmpty(menuIds)) {
            return false;
        }

        if (CollectionUtils.isNotEmpty(getMenuIdsByRole(roleId))) {
            authRoleMenuDAO.deleteByRoleId(roleId);
        }
        for (Long menuId : menuIds) {
            AuthRoleMenuEntity authRoleMenuEntity = new AuthRoleMenuEntity();
            authRoleMenuEntity.setRoleId(roleId);
            authRoleMenuEntity.setMenuId(menuId);

            authRoleMenuDAO.save(authRoleMenuEntity);
        }

        return true;
    }

    @Override
    public Boolean delete(Long id) {
        AuthMenuEntity authMenuEntity = authMenuDAO.selectById(id);
        if (authMenuEntity == null) {
            return false;
        }
        int num = authRoleMenuDAO.deleteByMenuId(authMenuEntity.getId());
        return authMenuDAO.deleteByMenuId(id) > 0;
    }
}
